package com.controller;

import com.pojo.book;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

public class showServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String uid=null;
        HttpSession session = req.getSession();
        Map<String, List<book>> car = (Map<String, List<book>>) session.getAttribute("car");
        Cookie[] cookies = req.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals("loginUser")){
                uid=cookies[i].getValue();
                break;
            }
        }
        if(car==null||car.size()==0){
            out.print("购物车没有任何东西");
            return;
        }else{
            if(uid!=null){
                out.print("<table border=\"1\" width=\"750px\">");
                out.print("<tr><td colspan=\"7\" align=\"center\">您的购物车：</td></tr>");
                double result=0.0;

                out.print("<tr \"><td>序号&#160</td> <td>图书名称&#160&#160&#160&#160&#160&#160</td> <td>数目&#160&#160&#160&#160</td> <td>单价&#160&#160&#160</td> <td>总价&#160&#160&#160</td> <td>删除&#160&#160&#160</td> <td>减少&#160&#160&#160</td>  </tr><br/>");

                List<book> buycar = car.get(uid);

                for (int i = 0; i < buycar.size(); i++) {
                    result+=buycar.get(i).getPrice()*buycar.get(i).getBookcount();
                    out.print("<tr>");
                    out.print("<td>"+buycar.get(i).getBookid()+"</td>");
                    out.print("<td>"+buycar.get(i).getBookName()+"</td>");
                    out.print("<td>"+buycar.get(i).getBookcount()+"</td>");
                    out.print("<td>"+buycar.get(i).getPrice()+"</td>");
                    out.print("<td>"+buycar.get(i).getPrice()*buycar.get(i).getBookcount()+"</td>");
                    out.print("<td><a href=\"deleteServlet?bookid="+buycar.get(i).getBookid()+"&page=2\">从购物车中移除</a></td>");
                    out.print("<form action=\"decrServlet\" method =\"post\">");
                    out.print("<td> <input type=\"hidden\" name=\"page\" value=\"2\"> <input type=\"text\" name=\"decr\"> </input> <input type=\"hidden\" name=\"bookid\" value=\""+buycar.get(i).getBookid()+"\"> </input>   <input type =\"submit\" value=\"提交\"> </td>");
                    out.print("</form>");
                    out.print("</tr>");
                }
                out.print("<tr><td colspan=\"4\">总价为：</td><td>"+result+"</td><td></td><td></td></tr>");

                out.print("</table>");
                out.print("<a href=\"purchaseServlet\">去结算("+buycar.size()+")</a>");


            }

        }







    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
